package com.luckmgr.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.utils.ArithUtil;
import com.common.utils.PropsLoader;
import com.common.utils.StringUtils;
import com.luckmgr.constant.LuckmgrConstant;
import com.luckmgr.service.ToolService;
import com.luckmgr.vo.BetRecord;



@Service
public class ToolServiceImpl implements ToolService {
	@Autowired
	private PropsLoader propsLoader;
	
	
	@Override
	public void betRecord(String activityid, 
			Map<String, List<String[]>> map) {
		List<BetRecord> bets = conversionRecord(map);
		
		
		for(BetRecord t : bets){
			System.out.println(t.getUserName()+"  "+t.getMoney()+"  "+t.getBetMoney());
		}
	}

	
	private List<BetRecord> conversionRecord(Map<String, List<String[]>> map){
		Map<String,BetRecord> rmap = new HashMap<String,BetRecord>();
		Iterator<Entry<String, List<String[]>>> iterator = map.entrySet().iterator();
		String indexkey =  propsLoader.props.getProperty(LuckmgrConstant.TZTOOL_EXCEL_USERKEY,"");
		String moneykey =  propsLoader.props.getProperty(LuckmgrConstant.TZTOOL_EXCEL_MONEY,"");
		String betmoneykey =  propsLoader.props.getProperty(LuckmgrConstant.TZTOOL_EXCEL_BETMONEY,"");
		while (iterator.hasNext()) {
			Entry<String, List<String[]>> entry = iterator.next();
			List<String[]> list = entry.getValue();
			if(list==null || list.size()==0) continue;
			int index = -1;
			int money = -1;
			int betmoney = -1;
			
			String[] haedstr = list.remove(0);
			for(int i=0;i<haedstr.length;i++){
				if(indexkey.equals(haedstr[i])){
					index = i;
				}else if(moneykey.equals(haedstr[i])){
					money = i;
				}else if(betmoneykey.equals(haedstr[i])){
					betmoney = i;
				}
			}
			if(index == -1) continue;
			
			for(String[] strs : list){
				String username = strs[index];
				username = username.trim();
				BetRecord tzbet = rmap.get(username);
				if(null == tzbet){
					tzbet = new BetRecord();
					tzbet.setUserName(username);
					rmap.put(username, tzbet)	;
				}
				
				if(money!= -1){
					Double d = StringUtils.findDouble(strs[money]);
					if(null != d){
						tzbet.setMoney(ArithUtil.add(tzbet.getMoney(),d));
					}
				}
				if(betmoney!= -1){
					Double d = StringUtils.findDouble(strs[betmoney]);
					if(null != d){
						tzbet.setBetMoney(ArithUtil.add(tzbet.getBetMoney(),d));
					}
				}
					
			}
			
		}
		return new ArrayList<BetRecord>(rmap.values());
	}
	
}
